﻿/*--------------------------------------------------------------
 * Author : Shine K Velayudhan 
 ---------------------------------------------------------------*/
using System;
using System.Threading;

namespace IQN.Logging
{
    public abstract class LogMessage
    {
        private static long nextSequenceNumber = DateTime.Now.Ticks;
        public DateTime LoggedOn { get; set; }
        public string MessageId { get; set; }
        public MessageLevel MessageLevel { get; set; }
        public string Source { get; set; }

        internal LogMessage(string messageSource, MessageLevel messageLevel)
        {
            this.Source = this.ClipAndClean(messageSource, 64);
            this.MessageLevel = messageLevel;
            this.MessageId = Guid.NewGuid().ToString("N").ToUpper();
            this.LoggedOn = DateTime.Now;
        }

        protected string ClipAndClean(string input, int maxLength)
        {
            if (null == input)
            {
                return string.Empty;
            }
            if (input.Length > maxLength)
            {
                return input.Substring(0, maxLength);
            }
            return input;
        }

        public override string ToString()
        {
            return this.MessageText;
        }

        public abstract string MessageText { get; }
        public abstract MessageType MessageType { get; }
        private static long NextSequenceNumber
        {
            get
            {
                return Interlocked.Increment(ref nextSequenceNumber);
            }
        }
    }
}
